package org.easychat.response;


import lombok.Data;
import lombok.Getter;

import java.io.Serializable;

/** 
 * @author Deyun
 * @date 2024/4/25 17:19
 */
public class ApiResult<T> extends BaseResponse implements Serializable {

    private T data;

    public ApiResult() {

    }

    public ApiResult(RestatusEnum rsp) {
        this.code = rsp.getCode();
        this.message = rsp.getMessage();
    }

    public ApiResult(RestatusEnum rsp, String message, T data) {
        this.code = rsp.getCode();
        this.message = message;
        this.data = data;
    }

    public ApiResult(RestatusEnum rsp, T data) {
        this.code = rsp.getCode();
        this.message = rsp.getMessage();
        this.data = data;
        this.setSuccess(this.code == RestatusEnum.SUCCESS.getCode());
    }


    public static <T> ApiResult<T> ok() {
        return new ApiResult<T>(RestatusEnum.SUCCESS);
    }

    public static <T> ApiResult<T> ok(String message, T data) {
        return new ApiResult<T>(RestatusEnum.SUCCESS, message, data);
    }

    public static <T> ApiResult<T> ok(T data) {
        return new ApiResult<T>(RestatusEnum.SUCCESS, data);
    }

    public static <T> ApiResult<T> fail() {
        return new ApiResult<T>(RestatusEnum.FAIL);
    }

    public static <T> ApiResult<T> fail(T data) {
        return new ApiResult<T>(RestatusEnum.FAIL, data);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
